import { defineStore } from "pinia";
import { fetchStaffByDeptId } from "@/api/system/staff";
import type { IStaff } from "@/api/system/staff";
import { ref, computed } from "vue";

export const useStaffStore = defineStore("staff", () => {
  const staffList = ref<IStaff[]>([]);
  const loading = ref(false);
  const error = ref<string | null>(null);

  // 获取部门员工
  const getStaffByDeptId = async (deptId: number) => {
    try {
      loading.value = true;
      error.value = null;
      const res = await fetchStaffByDeptId(deptId);
      staffList.value = res.data || [];
      // console.log("获取员工列表成功:", staffList.value);
    } catch (err: any) {
      error.value = `获取员工列表失败: ${err.message || '未知错误'}`;
      console.error("获取员工列表失败:", err);
      throw err;
    } finally {
      loading.value = false;
    }
  };

  return {
    staffList,
    loading,
    error,
    getStaffByDeptId,
  };
});